/**
 * 作者：New一个
 * 主页：http://www.newyige.com
 * 描述：主题选项jQuery代码
 * 时间：2016.05.16
 */

jQuery(document).ready(function($) {
	var upload_frame,
		value_id;

	//~ 上传文件
	$('.upload_button').on('click', function(event) {
		$(this).hide();
		$(this).siblings(".upload_delete").show();
		event.preventDefault();
		value_id = $(this).attr('id');
		if (upload_frame) {
			upload_frame.open();
			return;
		}
		upload_frame = wp.media({
			title: '插入文件',
			button: {
				text: '插入'
			},
			multiple: false
		});
		upload_frame.on('select', function() {
			var attachment = upload_frame.state().get('selection').first().toJSON();
			$('input[name=' + value_id + ']').val(attachment.url).trigger('change');
		});
		upload_frame.open();
	});

	//~ 显示文件类型图标
	$('.upload_images').on('change focus blur', function() {
		$select = '#' + $(this).attr('name') + '_preview';
		$value = $(this).val();
		if ($value) {
			var index1 = $value.lastIndexOf('.');
			var index2 = $value.length;
			var file_type = $value.substring(index1, index2);
			img_src = upload_preview.img_base;
			if ($.inArray(file_type, ['.png', '.jpg', '.gif', 'ico', '.bmp']) != '-1') {
				img_src = $value;
			} else if ($.inArray(file_type, ['.zip', '.rar', '.7z', '.gz', '.tar', '.bz', '.bz2']) != '-1') {
				img_src += upload_preview.img_path.archive;
			} else if ($.inArray(file_type, ['.mp3', '.wma', '.wav', '.mod', '.ogg', '.au']) != '-1') {
				img_src += upload_preview.img_path.audio;
			} else if ($.inArray(file_type, ['.avi', '.mov', '.wmv', '.mp4', '.flv', '.mkv']) != '-1') {
				img_src += upload_preview.img_path.video;
			} else if ($.inArray(file_type, ['.swf']) != '-1') {
				img_src += upload_preview.img_path.interactive;
			} else if ($.inArray(file_type, ['.php', '.js', '.css', '.json', '.html', '.xml']) != '-1') {
				img_src += upload_preview.img_path.code;
			} else if ($.inArray(file_type, ['.doc', '.docx', '.pdf', '.wps']) != '-1') {
				img_src += upload_preview.img_path._document;
			} else if ($.inArray(file_type, ['.xls', '.xlsx', '.csv', '.et', '.ett']) != '-1') {
				img_src += upload_preview.img_path.spreadsheet;
			} else if ($.inArray(file_type, ['.txt', '.rtf']) != '-1') {
				img_src += upload_preview.img_path._text;
			} else {
				img_src += upload_preview.img_path._default;
			}
			$file_view = '<img src ="' + img_src + '" />';
			$($select).html('').append($file_view);
		}
	});

	//~ 清除上传路径
	$(".upload_delete").click(function() {
		$(this).siblings(".upload_preview").children().remove();
		$(this).siblings(".upload_images").val("");
		$(this).hide();
		$(this).siblings(".upload_button").show();
	});

	//~ 判断上传是否为空
	if ($(".upload_images").val() === "") {
		$(".upload_delete").hide();
		$(".upload_button").show();
	} else {
		$(".upload_button").hide();
		$(".upload_delete").show();
	}

	//~ 颜色选择器
	$('.color_picker').wpColorPicker();

	//~ 主题设置选项卡
	$('.options_tabs').tabs();
});